package com.example.mallorder.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.example.mallorder.api.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yangtao
 * @version 1.0
 * @description: 用来测试了解Sentinel各种规则
 * @date 2021/7/9 0009 0:01
 */
@RestController
public class TestController {

  private int i = 0;

  @Autowired private TestService testService;

  @GetMapping("/order/message1")
  public String message1() {
    testService.message();
    return "message1";
  }

  @GetMapping("/order/message2")
  public String message2() {
    testService.message();
    return "message2";
  }

  @GetMapping("/order/message3")
  public String message3() {
    i++;
    // 异常比例0.333
    if (i % 3 == 0) {
      throw new RuntimeException("异常错误0.333");
    }
    return "message3";
  }

  @GetMapping("/order/message4/{name}/{age}")
  @SentinelResource("message4") // 注意这里必须使用这个注解标识,热点规则不生效
  public String message4(@PathVariable("name") String name, @PathVariable("age") String age) {
    return "message4";
  }
}
